from datetime import datetime
from app import db

# 优惠券模型
class Voucher(db.Model):
    __tablename__ = 'tb_voucher'
    
    voucher_id = db.Column(db.BigInteger, primary_key=True, autoincrement=True, comment='优惠券ID(主键)')
    name = db.Column(db.String(100), nullable=False, comment='优惠券名称')
    description = db.Column(db.String(255), comment='优惠券描述')
    discount_amount = db.Column(db.Numeric(10, 2), default=0.00, comment='优惠金额(元)')
    min_purchase = db.Column(db.Numeric(10, 2), default=0.00, comment='最低消费金额(元)')
    start_time = db.Column(db.DateTime, comment='开始时间')
    end_time = db.Column(db.DateTime, comment='结束时间')
    is_enabled = db.Column(db.Boolean, default=True, comment='是否启用(0-否,1-是)')
    create_time = db.Column(db.DateTime, default=datetime.now, comment='创建时间')
    
    def __repr__(self):
        return f'<Voucher {self.name}>'
    
    def to_dict(self):
        """转换为字典，用于API响应"""
        return {
            'voucher_id': self.voucher_id,
            'name': self.name,
            'description': self.description,
            'discount_amount': float(self.discount_amount) if self.discount_amount else 0.0,
            'min_purchase': float(self.min_purchase) if self.min_purchase else 0.0,
            'start_time': self.start_time.strftime('%Y-%m-%d %H:%M:%S') if self.start_time else None,
            'end_time': self.end_time.strftime('%Y-%m-%d %H:%M:%S') if self.end_time else None,
            'is_enabled': self.is_enabled,
            'create_time': self.create_time.strftime('%Y-%m-%d %H:%M:%S') if self.create_time else None
        }

# 用户优惠券模型
class UserVoucher(db.Model):
    __tablename__ = 'tb_user_voucher'
    
    user_voucher_id = db.Column(db.BigInteger, primary_key=True, autoincrement=True, comment='用户优惠券ID(主键)')
    user_id = db.Column(db.BigInteger, db.ForeignKey('tb_user.user_id', ondelete='CASCADE'), nullable=False, comment='用户ID')
    voucher_id = db.Column(db.BigInteger, db.ForeignKey('tb_voucher.voucher_id'), nullable=False, comment='优惠券ID')
    is_used = db.Column(db.Boolean, default=False, comment='是否已使用(0-否,1-是)')
    use_time = db.Column(db.DateTime, comment='使用时间')
    expire_time = db.Column(db.DateTime, comment='过期时间')
    create_time = db.Column(db.DateTime, default=datetime.now, comment='创建时间')
    
    # 关联
    voucher = db.relationship('Voucher', lazy=True)
    # 关联 - 使用back_populates与User类匹配
    user = db.relationship('User', back_populates='vouchers')
    
    def __repr__(self):
        return f'<UserVoucher {self.user_voucher_id}>'
    
    def to_dict(self):
        """转换为字典，用于API响应"""
        return {
            'user_voucher_id': self.user_voucher_id,
            'user_id': self.user_id,
            'voucher_id': self.voucher_id,
            'is_used': self.is_used,
            'use_time': self.use_time.strftime('%Y-%m-%d %H:%M:%S') if self.use_time else None,
            'expire_time': self.expire_time.strftime('%Y-%m-%d %H:%M:%S') if self.expire_time else None,
            'create_time': self.create_time.strftime('%Y-%m-%d %H:%M:%S') if self.create_time else None,
            'voucher': self.voucher.to_dict() if self.voucher else None
        } 